package com.ruoyi.device.history.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.device.history.domain.BizDeviceHistory;
import com.ruoyi.device.history.service.IBizDeviceHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 设备监测历史表
 * @author breeze
 * @date 2022-12-15
 */
@RestController
@RequestMapping("/device/history")
public class BizDeviceHistoryController extends BaseController {
    
    @Autowired
    private IBizDeviceHistoryService bizDeviceHistoryService;

    @GetMapping("/list")
    public TableDataInfo<?> list(BizDeviceHistory bizDeviceHistory){
        startPage();
        List<BizDeviceHistory> list = bizDeviceHistoryService.selectBizDeviceHistoryList(bizDeviceHistory);
        return getDataTable(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(bizDeviceHistoryService.selectBizDeviceHistoryById(id));
    }

    @Log(title = "设备监测历史表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizDeviceHistory bizDeviceHistory){
    	bizDeviceHistory.setCreateTime(new Date());
    	bizDeviceHistory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(bizDeviceHistoryService.save(bizDeviceHistory));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody BizDeviceHistory bizDeviceHistory){
    	bizDeviceHistory.setUpdateTime(new Date());
    	bizDeviceHistory.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(bizDeviceHistoryService.updateById(bizDeviceHistory));
    }

    /**
     * 传入@PathVariable的格式类似为/1,2,3,4,5时,会自动转为数组
     */
    @Log(title = "设备监测历史表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(bizDeviceHistoryService.removeByIds(Arrays.asList(ids)));
    }
    
    /**
     * 导出设备监测历史表列表
     */
    @Log(title = "设备监测历史表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizDeviceHistory bizDeviceHistory){
        List<BizDeviceHistory> list = bizDeviceHistoryService.selectBizDeviceHistoryList(bizDeviceHistory);
        ExcelUtil<BizDeviceHistory> util = new ExcelUtil<BizDeviceHistory>(BizDeviceHistory.class);
        util.exportExcel(response, list, "设备监测历史表数据");
    }
    
}
